if left(&visiX,1)='0'
   if SUBSTR(&visiX,1,2)='00'
      &VISIO=SUBSTR(&VISIX,2,5)
   ELSE
      &visiO=substr(&visiX,2,4)
   endif
else
   &visiO=left(&visiX,5)
endif
VISIN=DTOS(&TDATE)+&VISIO
CNT=0
SELE 4
@ 13, 1 clea to 23,78
SET COLO TO GR+/B
@ 13, 4 say RANKV+" LAST VISIT"
@ 13,24 say "Date of Visit :"
@ 13,54 say "Invoice Number :"
@ 13,40 say &TDATE
@ 13,71 say &visiO
go top
cr=0
DO WHILE .T.
   SEEK VISIN
   if eof()
      SET COLOR TO GR+/B
      @ 16,17 SAY "NO MEDICAL PROCEDURE RECORDED DURING THIS VISIT"
      SET COLOR TO BG+/B,W+/N
      EXIT
   else
      DO WHILE INV_NBER=&VISIO
         cnt=cnt+1
         skip
      ENDDO
      if cnt=0
         EXIT
      endif
      set colo to bg+/b+
      declare VISI[cnt]
      DECLARE FILD1[CNT]
      cr=0
      go top
      SEEK VISIN
      DO WHILE INV_NBER=&VISIO
         CR=CR+1
         cri=str(cr,4,0)
         DCOD=DOC_CODE
         IF DCOD<>SPACE(3)
            SELE 6
            SET EXCLU OFF
            USE &DR1&F400
            LOCA FOR DOC_CODE=DCOD
            IF FOUND()
               PERFORM='By Dr. '+DOC_ALIAS
            ELSE
               PERFORM=SPACE(19)
            ENDIF
            SELE 4
         ELSE
            PERFORM=SPACE(19)
         ENDIF
         ARCO=AREA_CODE
         SELE 7
         SET EXCLU OFF
         USE &DR1&F100
         LOCA FOR AREA_CODE=ARCO
         IF FOUND()
            AREA=MAIN_AREA
         ENDIF
         SELE 4
         fild1[cr]=AREA_CODE+' '+SUB_AREACO+' '+PRO_CODE+'  '+AREA+'  '+SHRT_DESCR+'  '+PERFORM
         skip
      enddo
      DO WHILE .T.
         set colo to bg+/b
         SET COLO TO BG+/B,W+/N
         @ 15,3 clear to 23,78
         do pcon60
         SET COLO TO W+/B+
         inkey()
         cari=achoice(15,2,17,77,fild1)
         mx=lastkey()
         IF MX=13 .OR. MX=27
            EXIT
         ELSE
            LOOP
         ENDIF
      ENDDO
      EXIT
   endif
ENDDO
CNT=0
@ 23,1 CLEA TO 23,78
SELE 5
DO WHILE .T.
   SEEK visiN
   if eof()
      SET COLOR TO GR+/B
      @ 20,17 SAY "NO PHARMACY DELIVERY RECORDED DURING THIS VISIT"
      do pcon60
      SET COLOR TO BG+/B, W/N
      exit
   else
      DO WHILE INV_NBER=&visiO
         cnt=cnt+1
         skip
      ENDDO
      if cnt=0
         EXIT
      endif
      set colo to bg+/b+
      declare VISI[cnt]
      DECLARE FILD1[CNT]
      cr=0
      go top
      SEEK visiN
      DO WHILE INV_NBER=&visiO
         CR=CR+1
         cri=str(cr,4,0)
         PCOD=PHAR_CODE
         SELE 8
         SET EXCLU OFF
         USE &DR1&F200 INDEX &DR1&F200
         SEEK PCOD
         IF EOF()
            SELE 8
            SET EXCLU OFF
            USE &DR1&F300 INDEX &DR1&F300
            SEEK PCOD
            IF .NOT. EOF()
               PNAM=DISP_NAME
               PTYP=DISP_TYPE
               PQAN=DISP_QANT
               SUNT=SELL_UNIT
***
***  Addeed by dickson 5/12/98
***    This is to handle the situation where the Drugs/ Disposable used
***    during the previous Patient's Consult. History cound not be found
***    be found - due to the fact the drug has been deleted from the
***    the master list
***    Assign Blanks if not found in the drugs/disposable master list
***
            ELSE               
               PNAM=PCOD
               PTYP=" "
               PQAN=" "
               SUNT=" "
***
***     end of amendment - dickson 5/12/98
***     

            ENDIF
         ELSE
            PNAM=DRUG_NAME
            PTYP=DRUG_TYPE
            PQAN=DRUG_QANT
            SUNT=SELL_UNIT
         ENDIF

         SELE 5
         fild1[cr]=PCOD+'  '+PNAM+'  '+PTYP+'  '+PQAN+'   '+str(PHAR_X,0)+' '+SUNT
         skip
      enddo
      DO WHILE .T.
         set colo to bg+/b
         SET COLO TO BG+/B,W+/N
         @ 19,3 clear to 23,78
         do pcon60
         SET COLO TO W+/B+
         inkey()
         cari=achoice(19,2,21,77,fild1)
         mx1=lastkey()
         IF MX1=13 .OR. MX1=27
            EXIT
         ELSE
            LOOP
         ENDIF
      ENDDO
      exit
   endif
ENDDO
